package com.mine.yyb.modules.sys.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.mine.yyb.common.enums.YybModulesEnum;
import com.mine.yyb.common.utils.exception.MineErrorCode;
import com.mine.yyb.common.utils.exception.MineException;
import com.mine.yyb.common.utils.execl.MineExcelUtils;
import com.mine.yyb.common.web.YybBaseController;
import com.mine.yyb.modules.sys.bo.CnamzReportBO;
import com.mine.yyb.modules.sys.bo.ReportBO;
import com.mine.yyb.modules.sys.enums.SysCodeEnum;
import com.mine.yyb.modules.sys.service.IReportService;
import com.mine.yyb.modules.sys.vo.ReportVO;

/**
 * <pre>
 * <一句话功能简述>
 * 
 * 商家反馈报表数据control类
 * 
 * <功能详细描述>
 * </pre>
 * 
 * @author xuganhua
 * @version [版本号, 2016年1月2日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@RequestMapping(value = "/sys/report")
public class ReportControl extends YybBaseController
{
    @Autowired
    private IReportService iReportService;
    
    public static void main(String[] args)
        throws Exception
    {
        
        File file = new File("f://slof77test.xlsx");
        
        if (file.exists())
        {
            MineExcelUtils mineImportExcel = new MineExcelUtils(file, 7, 3);
            List<CnamzReportBO> reportList = mineImportExcel.getDataList(CnamzReportBO.class);
            for (CnamzReportBO reportBO : reportList)
            {
                if(reportBO.getOrderID().startsWith("C"))
                {
                    System.out.println(reportBO);
                }
            }
        }
        
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 导入execl数据入库
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2016年1月2日]
     * @param file
     * @param mode
     * @param request
     * @param response
     * @param session
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/importExcel")
    public ResponseEntity<String> importExcel(MultipartFile file, HttpServletRequest request,
        HttpServletResponse response, HttpSession session)
    {
        // 响应码
        String retCode = null;
        
        try
        {
            // 文件为空
            if (file == null)
            {
                throw new MineException(SysCodeEnum.ERROR_REPORT_FILE_EMPTY);
            }
            
            // 文件大小超过5M
            if (file.getSize() > 5120 * 1024)
            {
                throw new MineException(SysCodeEnum.ERROR_FILE_SIZE_TOO_LARGE);
            }
            
            MineExcelUtils mineImportExcel = new MineExcelUtils(file, -1, 0);
            // 读取excel文件里面的数据
            List<ReportBO> reportList = mineImportExcel.getDataList(ReportBO.class);
            List<ReportBO> errorList = Lists.newArrayList();
            if (reportList.size() > 0)
            {
                for (ReportBO reportBO : reportList)
                {
                    if (StringUtils.isBlank(reportBO.getOrderID()))
                    {
                        errorList.add(reportBO);
                        continue;
                    }
                    String result = null;
                    ReportVO temp = iReportService.queryReport(reportBO);
                    if (temp != null)
                    {
                        result = iReportService.updateReport(reportBO);
                    }
                    else
                    {
                        result = iReportService.saveReport(reportBO);
                    }
                    if (MineErrorCode.SUCCESS.name().equals(result))
                    {
                        errorList.add(reportBO);
                        continue;
                    }
                }
            }
            retCode = MineErrorCode.SUCCESS.name();
        }
        catch (MineException e)
        {
            logger.error(YybModulesEnum.SYS.name(), e);
            retCode = e.getErrorCode().name();
        }
        catch (Exception e)
        {
            logger.error(YybModulesEnum.SYS.name(), e);
            retCode = MineErrorCode.ERROR_INTERNAL.name();
        }
        
        return getResponseEntity(retCode, null);
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 导入中亚报表
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author 徐干华 0118
     * @version [版本号, 2017年5月5日]
     * @param file
     * @param request
     * @param response
     * @param session
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/importCnamzExcel")
    public ResponseEntity<String> importCnamzExcel(MultipartFile file, HttpServletRequest request,
        HttpServletResponse response, HttpSession session)
    {
        // 响应码
        String retCode = null;
        
        try
        {
            // 文件为空
            if (file == null)
            {
                throw new MineException(SysCodeEnum.ERROR_REPORT_FILE_EMPTY);
            }
            
            // 文件大小超过5M
            if (file.getSize() > 5120 * 1024)
            {
                throw new MineException(SysCodeEnum.ERROR_FILE_SIZE_TOO_LARGE);
            }
            
            MineExcelUtils mineImportExcel = new MineExcelUtils(file, -1, 3);
            // 读取excel文件里面的数据
            List<CnamzReportBO> reportList = mineImportExcel.getDataList(CnamzReportBO.class);
            List<CnamzReportBO> errorList = Lists.newArrayList();
            if (reportList.size() > 0)
            {
                for (CnamzReportBO reportBO : reportList)
                {
                    if (StringUtils.isBlank(reportBO.getOrderID()))
                    {
                        errorList.add(reportBO);
                        continue;
                    }
                    String result = null;
                    ReportVO temp = iReportService.queryCnamzReport(reportBO);
                    if (temp != null)
                    {
                        result = iReportService.updateCnamzReport(reportBO);
                    }
                    else
                    {
                        result = iReportService.saveCnamzReport(reportBO);
                    }
                    if (MineErrorCode.SUCCESS.name().equals(result))
                    {
                        errorList.add(reportBO);
                        continue;
                    }
                }
            }
            retCode = MineErrorCode.SUCCESS.name();
        }
        catch (MineException e)
        {
            logger.error(YybModulesEnum.SYS.name(), e);
            retCode = e.getErrorCode().name();
        }
        catch (Exception e)
        {
            logger.error(YybModulesEnum.SYS.name(), e);
            retCode = MineErrorCode.ERROR_INTERNAL.name();
        }
        
        return getResponseEntity(retCode, null);
    }
    
}
